package appbase.third.xinge;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;

import com.tencent.xinge.XingeApp;
import com.tencent.xinge.bean.Alert;
import com.tencent.xinge.bean.Aps;
import com.tencent.xinge.bean.AudienceType;
import com.tencent.xinge.bean.Environment;
import com.tencent.xinge.bean.Message;
import com.tencent.xinge.bean.MessageAndroid;
import com.tencent.xinge.bean.MessageIOS;
import com.tencent.xinge.bean.MessageType;
import com.tencent.xinge.bean.Platform;
import com.tencent.xinge.push.app.PushAppRequest;

public class XingePushKit {

	
	private static String android_app_id = "58fde63dcfd5f";
	private static String android_secret_key = "98c3e5ee30cd6c6406ca5ba1626705e9";
	
	private static String ios_app_id = "cc3e3ff4a5511";
	private static String ios_secret_key = "5b11490447baaaec35039188d20bc2e0";
	private static Environment ios_environment = Environment.dev;
	
	private static String push_title = "推送标题";
	
	private static int account_list_max_size = 1000;
	
	public static void init(String androidAppId, String androidSecretKey, String iosAppId, String iosSecretKey, String iosEnvironment, String pushTitle)
	{
		android_app_id = androidAppId;
		android_secret_key = androidSecretKey;
		
		ios_app_id = iosAppId;
		ios_secret_key = iosSecretKey;
		push_title = pushTitle;
		
		if (iosEnvironment.equals("product"))
		{
			ios_environment = Environment.product;
		}else
		{
			ios_environment = Environment.dev;
		}
	}
	
	
	public static PushResult pushNotifyAccountIOS(String content, String customContent, String account)
	{
		String title = push_title;
		
		ArrayList<String> accountList = new ArrayList<String>();
		accountList.add(account);
		
		if (customContent == null)
		{
			customContent = "";
		}
		MessageIOS ios = createMessageIOS(title, content, customContent);
		return push(title, content, null, ios, accountList, Platform.ios, ios_environment, MessageType.notify, AudienceType.account, ios_app_id, ios_secret_key);
	}
	
	/**
	 * accountList.size() <= 1000
	 */
	public static void pushNotifyAccountListIOS(String content, String customContent, ArrayList<String> accountList)
	{
		if (accountList.size() > account_list_max_size)
		{
			List<ArrayList<String>> list = toValidAccountList(accountList);
			for (ArrayList<String> arrayList : list) 
			{
				pushNotifyAccountListIOS(content, customContent, arrayList);
			}
			return;
		}
		if (customContent == null)
		{
			customContent = "";
		}
		
		String title = push_title;
		MessageIOS ios = createMessageIOS(title, content, customContent);
		push(title, content, null, ios, accountList, Platform.ios, ios_environment, MessageType.notify, AudienceType.account_list, ios_app_id, ios_secret_key);
	}
	
	
	public static PushResult pushNotifyAccountAndroid(String content, String customContent, String account)
	{
		String title = push_title;
		
		ArrayList<String> accountList = new ArrayList<String>();
		accountList.add(account);
		
		if (customContent == null)
		{
			customContent = "";
		}
		MessageAndroid android = createMessageAndroid(customContent);
		return push(title, content, android, null, accountList, Platform.android, ios_environment, MessageType.notify, AudienceType.account, android_app_id, android_secret_key);
	}
	
	
	public static void pushNotifyAccountListAndroid(String content, String customContent, ArrayList<String> accountList)
	{
		if (accountList.size() > account_list_max_size)
		{
			List<ArrayList<String>> list = toValidAccountList(accountList);
			for (ArrayList<String> arrayList : list) 
			{
				pushNotifyAccountListAndroid(content, customContent, arrayList);
			}
			return;
		}
		if (customContent == null)
		{
			customContent = "";
		}
		
		String title = push_title;
		MessageAndroid android = createMessageAndroid(customContent);
		push(title, content, android, null, accountList, Platform.android, ios_environment, MessageType.notify, AudienceType.account_list, android_app_id, android_secret_key);
	}
	
	
	private static PushResult push(String title, String content, MessageAndroid android, MessageIOS ios, ArrayList<String> accountList, Platform platform, Environment iosEnvironment, MessageType messageType, AudienceType audienceType, String appId, String secretKey)
	{
		XingeApp xinge = new XingeApp(appId, secretKey);
		
		PushAppRequest request = new PushAppRequest();
		
		request.setPlatform(platform);
		request.setEnvironment(iosEnvironment);
		request.setMessage_type(messageType);
		
		request.setAudience_type(audienceType);
		request.setAccount_list(accountList);
		
		Message message = new Message();
		message.setTitle(title);
		message.setTitle(content);
		
		if (android != null)
		{
			message.setAndroid(android);
		}
		
		if (ios != null)
		{
			message.setIos(ios);
		}
		
		request.setMessage(message);
		
		JSONObject resultJson = xinge.pushApp(request);
		System.out.println(resultJson.toString());
		return PushResult.jsonToObj(resultJson);
	}
	
	
	private static MessageAndroid createMessageAndroid(String custom_content) 
	{
		MessageAndroid android = new MessageAndroid();
		android.setCustom_content(custom_content);
		return android;
	}
	
	
	private static MessageIOS createMessageIOS(String title, String content, String custom_content) 
	{
		Alert alert = new Alert();
		alert.setTitle(title);
		alert.setBody(content);
		
		Aps aps = new Aps();
		aps.setAlert(alert);
		aps.setBadge_type(-2);//1) -1:角标数字不变； 2) -2:角标数字自动加1； 3) >=0:设置「自定义」角标数字
		
		MessageIOS ios = new MessageIOS();
		ios.setCustom(custom_content);
		ios.setAps(aps);
		
		return ios;
	}
	
	private static List<ArrayList<String>> toValidAccountList(ArrayList<String> accountList)
	{
		int size = accountList.size();
		int subCnt = size / account_list_max_size + (size % account_list_max_size > 0 ? 1 : 0); 
		
		List<ArrayList<String>> list = new ArrayList<ArrayList<String>>();
		for (int i = 0; i < subCnt; i++) 
		{
			int fromIndex = i * account_list_max_size;
			int toIndex = (i + 1) * account_list_max_size;
			if (toIndex > size)
			{
				toIndex = size;
			}
			ArrayList<String> subList = (ArrayList<String>)accountList.subList(fromIndex, toIndex);
			list.add(subList);
		}
		
		return list;
	}
	
}
